package org.itstack.filter;

import org.itstack.BaseOrderLogic;
import org.itstack.model.Order;
import org.itstack.OrderLogicFilter;
import org.itstack.model.vo.TreeNodeLink;

import java.util.List;

/**
 * 复合订单过滤器
 * 组合多个条件的过滤器
 */
public class CompositeOrderFilter extends BaseOrderLogic {
    
    private List<OrderLogicFilter> filters;
    
    public CompositeOrderFilter(List<OrderLogicFilter> filters) {
        this.filters = filters;
    }
    
    @Override
    public String matterValue(Order order) {
        // 组合多个过滤器的结果
        StringBuilder result = new StringBuilder();
        for (OrderLogicFilter filter : filters) {
            result.append(filter.matterValue(order)).append("_");
        }
        return result.toString().replaceAll("_$", "");
    }
    
    @Override
    public Long filter(String matterValue, List<TreeNodeLink> treeNodeLinkList) {
        // 自定义复合决策逻辑
        String[] values = matterValue.split("_");
        
        // 复合决策逻辑：所有条件都满足才通过
        for (TreeNodeLink nodeLink : treeNodeLinkList) {
            if (evaluateCompositeLogic(values, nodeLink)) {
                return nodeLink.getNodeIdTo();
            }
        }
        
        return 0L;
    }
    
    private boolean evaluateCompositeLogic(String[] values, TreeNodeLink nodeLink) {
        // 这里可以实现更复杂的复合逻辑
        // 例如：至少满足2个条件，或者所有条件都满足等
        String expectedValue = nodeLink.getRuleLimitValue();
        
        // 简单实现：检查是否包含期望值
        for (String value : values) {
            if (value.equals(expectedValue)) {
                return true;
            }
        }
        
        return false;
    }
}
